
import javax.microedition.lcdui.*;
import net.sourceforge.floggy.persistence.FloggyException;
import net.sourceforge.floggy.persistence.PersistableManager;
import net.sourceforge.floggy.persistence.SingleObjectSet;

public class Recordes extends Canvas {

    static Record[] pessoas = new Record[]{new Record("Diego", 50), new Record("Pedro", 40), new Record("Maria", 30), new Record("Tiago", 20), new Record("Marcos", 10)};

    public Recordes() {
        System.out.println("Recordes construtor");

        try {
            readData();
        } catch (FloggyException ex) {
            //Problema ao carregar
        }
       
        System.out.println("Passou readData()");

        setFullScreenMode(true);
    }

    private void readData() throws FloggyException {
        System.out.println("readData()");
        
        PersistableManager pm = PersistableManager.getInstance();
        System.out.println("Passou pm");
        
        SingleObjectSet result = pm.find(Record.class, null, null);
        System.out.println("passou result");
        
        System.out.println("Size " + result.size());
       // pessoas = new Record[result.size()];

        for (int i = 0; i < result.size(); i++) {
            pessoas[i] = (Record) result.get(i);
            System.out.println("Carrega " + pessoas[i].nome + " " + pessoas[i].pontos);
        }

        repaint();
    }

    public void check(final Player p) {
        for (int i = 0; i < pessoas.length; i++) {
            final int lugar = i;

            if (p.getPontos() > pessoas[i].pontos) {
                final TextBox tb = new TextBox("Seu nome", null, 100, TextField.ANY);
                tb.addCommand(new Command("Ok", Command.OK, 0));
                tb.setCommandListener(new CommandListener() {

                    public void commandAction(Command c, Displayable d) {
                        Record nr = new Record();
                        nr.nome = tb.getString();
                        nr.pontos = p.getPontos();
                        Record ant;

                        for (int i = lugar; i < 5; i++) {
                            ant = pessoas[i];
                            pessoas[i] = nr;
                            nr = ant;
                        }

                        try {
                            PersistableManager.getInstance().delete(nr);
                        } catch (FloggyException ex) {
                            ex.printStackTrace();
                        }
                        
                        for (int i = 0; i < pessoas.length; i++) {

                            try {
                                System.out.println("Save " + pessoas[i].nome + " " + pessoas[i].pontos);
                                pessoas[i].save();
                            } catch (FloggyException ex) {
                                //TOdo problema ao salvar
                            }

                        }

                        DMath.getInstance().getDisplay().setCurrent(new Recordes());
                    }
                });

                DMath.getInstance().getDisplay().setCurrent(tb);
                return;
            }
        }

        Menu m = new Menu();
        DMath.getInstance().getDisplay().setCurrent(m);
    }

    public void paint(Graphics g) {

        g.setColor(0);
        g.fillRect(0, 0, getWidth(), getHeight());

        String titulo = "Recordes";

        int h = g.getFont().getHeight();
        int w = g.getFont().stringWidth(titulo);

        Game.drawBevelRect(g, 0, 0, getWidth(), getHeight(), 0x222222,
                0x444444, 0x666666);
        Game.drawBevelRect(g, 4, h + 1, getWidth() - 8, getHeight()
                - (4 + h + 1), 0x666666, 0x000000, 0x222222);

        g.setColor(0x00ff50);
        g.drawString(titulo, (getWidth() - w) / 2, 1, 0);

        g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN,
                Font.SIZE_SMALL));
        g.setColor(0x00ff00);

        for (int i = 0; i < pessoas.length; i++) {
            String text = pessoas[i].nome + ":  " + pessoas[i].pontos;
            g.drawString(text,
                    ((getWidth() - g.getFont().stringWidth(text)) / 2),
                    ((getHeight() - g.getFont().getHeight() * 5) / 2 + 2)
                    + (i * h), 0);
        }

    }

    protected void keyPressed(int k) {
        DMath.getInstance().getDisplay().setCurrent(new Menu());
    }

    protected void pointerPressed(int x, int y) {
        DMath.getInstance().getDisplay().setCurrent(new Menu());
    }

}
